package com.hzpd.hainan.module.news.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzpd.hainan.R;
import com.hzpd.hainan.model.news.NewsBean;
import com.hzpd.hainan.model.news.SubjectItemColumnsBean;
import com.hzpd.hainan.utils.ViewHolder;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

/**
 * Created by 猪猪侠 on 2017/7/7.
 */

public class ZhuantiDetailListAdapter extends BaseAdapter{
    private Activity context;
    private LayoutInflater inflater;


    private LinkedHashMap<SubjectItemColumnsBean, List<NewsBean>> columnList;

    public ZhuantiDetailListAdapter(Activity context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        columnList = new LinkedHashMap<SubjectItemColumnsBean, List<NewsBean>>();
    }
    public void appendData(SubjectItemColumnsBean column, List<NewsBean> list,
                           Boolean isClearOldList) {

        List<NewsBean> oldList = columnList.get(column);
        if (null == oldList) {
            oldList = new ArrayList<NewsBean>();
        }
        else {
            if (isClearOldList) {
                oldList.clear();
            }
        }
        oldList.addAll(list);
        columnList.put(column, oldList);
    }
    public void clearData(){
        columnList.clear();
    }
    @Override
    public int getCount() {
        if (0 == columnList.size()) {
            return 0;
        } else {
            int columnCounts = 0;
            Set<SubjectItemColumnsBean> sets = columnList.keySet();
            for (SubjectItemColumnsBean sicb : sets) {
                columnCounts += 1;
                List<NewsBean> nbList = columnList.get(sicb);
                if (null != nbList) {
                    columnCounts += nbList.size();
                }
            }
            return columnCounts;
        }

    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        int columnCounts = 0;
        Set<SubjectItemColumnsBean> sets = columnList.keySet();
        for (SubjectItemColumnsBean sicb : sets) {
            if (position == columnCounts) {
                return 0;
            } else {
                columnCounts += 1;
                List<NewsBean> nbList = columnList.get(sicb);
                if (null != nbList && nbList.size() > 0) {
                    columnCounts += nbList.size();
                    if (position < columnCounts) {
                        return 1;
                    }
                }
            }
        }

        return 0;
    }
    @Override
    public NewsBean getItem(int position) {
        NewsBean nb = null;
        int columnCounts = 0;
        Set<SubjectItemColumnsBean> sets = columnList.keySet();
        for (SubjectItemColumnsBean sicb : sets) {
            if (position == columnCounts) {
                NewsBean bean=new NewsBean();
                bean.setTitle(sicb.getCname());
                return bean;
            } else {
                columnCounts += 1;
                List<NewsBean> nbList = columnList.get(sicb);
                if (null != nbList && nbList.size() > 0) {
                    if (position >= columnCounts
                            && position < columnCounts + nbList.size()) {
                        nb = nbList.get((position - columnCounts)
                                % nbList.size());
                        break;
                    }
                    columnCounts += nbList.size();
                }
            }
        }

        return nb;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NewsBean nb = (NewsBean) getItem(position);
        int type = getItemViewType(position);
        if (null == convertView) {
            if (0 == type) {
                convertView = inflater.inflate(R.layout.zhuanti_column_item,
                        parent, false);
            }else{
                convertView = inflater.inflate(R.layout.lehuo_list_item_layout,
                        parent, false);
            }
        }
        if (0 == type) {
            TextView zhuanti_tv_column = ViewHolder.get(convertView,
                    R.id.zhuanti_tv_column);
            NewsBean bean =  getItem(position);
            String title = bean.getTitle();
            zhuanti_tv_column.setText("" + title);
        } else
        {
            TextView newsitem_time = ViewHolder
                    .get(convertView, R.id.newsitem_time);
            TextView title = ViewHolder
                    .get(convertView, R.id.lehuo_content_txt);
            TextView viewnumber__id = ViewHolder.get(convertView, R.id.viewnumber__id);
            TextView praisenumber_id = ViewHolder.get(convertView, R.id.praisenumber_id);
            ImageView img = ViewHolder.get(convertView, R.id.lehuo_img_id);
            List<String> s= nb.getImgs();
            String simg = "";
            if (null != s && s.size() > 0) {
                simg =s.get(0);
            }
            Glide.with(context).load(simg).crossFade().placeholder(R.drawable.default_bg).into(img);
            newsitem_time.setText(nb.getUpdateTime());
            title.setText(nb.getTitle());
            viewnumber__id.setText(nb.getViewnum());
            praisenumber_id.setText(nb.getPraisenum());
        }


        return convertView;
    }
}
